/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Project;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;

/**
 *
 * @author R. Kasmani
 */
@Named
public class SearchBean implements Serializable {

    @Resource(name = "jdbc/project")
    private DataSource ds;
    private String username = "";
    private String make = "";
    private String model = "";
    private String color = "";
    private int year = 0;
    private int miles = 0;
    private int price = 0;
    private boolean searched = false;

    public SearchBean() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String newValue) {
        this.username = newValue;
    }

    public String getMake() {
        return this.make;
    }

    public void setMake(String newValue) {
        this.make = newValue;
    }

    public String getModel() {
        return this.model;
    }

    public void setModel(String newValue) {
        this.model = newValue;
    }

    public String getColor() {
        return this.color;
    }

    public void setColor(String newValue) {
        this.color = newValue;
    }

    public int getYear() {
        return this.year;
    }

    public void setYear(int newValue) {
        this.year = newValue;
    }

    public int getMiles() {
        return this.miles;
    }

    public void setMiles(int newValue) {
        this.miles = newValue;
    }

    public void setPrice(int newValue) {
        this.price = newValue;
    }

    public int getPrice() {
        return this.price;
    }

    public boolean getSearched() {
        return this.searched;
    }

    public void setSearched(boolean newBool) {
        this.searched = newBool;
    }

    /*
     * @param carID, make, model, year, price, mileage, description and username of car
     * @return car object
     * creates a car object with the given attributes.
     */
    public CarBean buildCarListing(int carID, String username, String make, String model, String color, int year, int price,
            int mileage, String description) {
        CarBean car = new CarBean();
        car.setMake(make);
        car.setModel(model);
        car.setColor(color);
        car.setMiles(mileage);
        car.setPrice(price);
        car.setYear(year);
        car.setDescription(description);
        car.setUsername(username);
        return car;
    }
    /*
     * @param Result Set: result set holding cars from car table.
     * @return list of car objects.
     * Builds a list of car objects from a result set.
     */
    public List<CarBean> buildCarsList(ResultSet rs) throws SQLException {
        List<CarBean> list = new ArrayList<CarBean>();

        //if result set has more rows add to list.
        if (rs.next()) {
            list.add(buildCarListing(
                    rs.getInt("carID"),
                    rs.getString("carID"),
                    rs.getString("make"),
                    rs.getString("model"),
                    rs.getString("color"),
                    rs.getInt("year"),
                    rs.getInt("price"),
                    rs.getInt("miles"),
                    rs.getString("description")));
        }
        return list;
    }

    public ResultSet searchCar(String make, String model, int year, String color, int miles, int price, String username)
            throws SQLException {

        /* Create search string depending on search parameters */
        /* result set returns:
         * listing ID
         * car's make
         * car's model
         * car's color
         * seller's username
         * year of the car
         * car's mileage
         * price of car
         * description of car
         */
        String searchQuery = "SELECT car.carID, \n"
                + "car.make,\n"
                + "car.model,\n"
                + "car.\"year\",\n"
                + "car.color,\n"
                + "car.price,\n"
                + "car.mileage,\n"
                + "car.description,"
                + "users.username,"
                + "FROM cars, users,";
        if (make != "" && model != "" && year != 0 && color != "" && username != "") {
            searchQuery.concat("WHERE");
        }
        if (make != "") {
            searchQuery.concat("AND car.make LIKE %" + make + "%");
        }
        if (model != "") {
            searchQuery.concat("AND car.model LIKE %" + model + "%");
        }
        if (Integer.toString(year) != "") {
            searchQuery.concat("AND car.year >=" + year);
        }
        if (Integer.toString(miles) != "") {
            searchQuery.concat("AND car.miles >=" + miles);
        }
        if (Integer.toString(price) != "") {
            searchQuery.concat("And car.price <=" + price);
        }
        if (color != "") {
            searchQuery.concat("AND car.color LIKE %" + color + "%");
        }
        if (username != "") {
            searchQuery.concat("AND users.username LIKE %" + username + "%");
        }

        Connection connection = ds.getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery(searchQuery);
            //return buildCarsList(result);
            CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();
            crs.populate(result);
            return crs;

        } catch (SQLException e) {
            System.out.println("SQL ERROR: SEARCH LISTINGS");
        } finally {
            connection.close();
        }
        return null;
    }

    /*
     * Function for running search on web page, redirects to search page.
     */
    public String userSearchAction() throws SQLException {
        searchCar(this.make, this.model, this.year, this.color, this.miles, this.price, this.username);
        searched = true;
        return "search";
    }
}
